<template>
  <div class="app-container calendar-list-container">

    <el-table v-loading="!pload" @selection-change="selectionChange" ref="multipleTable" border :data="tableData">
      <el-table-column label="文章标题" prop="articleTitle" align="center" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="内容" prop="content" align="center" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="回复评论ID" prop="commentId" align="center" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="用户名称" prop="fromUserName" align="center" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="状态" width="100" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-tag :type="['success','danger'][scope.row.status]" size="mini">{{['正常','回收站'][scope.row.status]}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column property="url" label="创建时间" show-overflow-tooltip width="200">
        <template slot-scope="scope">
          <span>{{scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}')}}</span>
          </template>
      </el-table-column>
      <el-table-column <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">查看</el-button>
          <el-button size="mini" @click="handleRecover(scope.$index, scope.row)" v-if="scope.row.status === 1">恢复</el-button>
          <el-button size="mini" @click="handleDelete(scope.$index, scope.row)" v-if="scope.row.status === 0">删除</el-button>
       </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-size="limit" :page-sizes="limits" background layout="sizes, prev, pager, next, jumper, total" :total="count">
      </el-pagination>
    </div>
    <edit v-if="edit" :data="selectTable" @getData="getData"></edit>
  </div>
</template>
<script>
import * as api from '@/utils/api'
import waves from '@/directive/waves' // 水波纹指令
import edit from './edit'
export default {
  directives: {
    waves
  },
  data() {
    return {
      tableData: [],
      search: {
        keyword: '',
        type: -1
      },
      selection: [],
      selectTable: {},
      pload: false,
      edit: false,
      currentPage: 1,
      limit: 10,
      limits: [10, 20, 50, 100],
      count: 0
    }
  },
  computed: {
    userId () {
      return this.$route.params.id
    }
  },
  components: {
    edit
  },
  created() {
    this.getData()
  },
  methods: {
    selectionChange(selection) {
      this.selection = selection
    },
    toggleSelection(s) {
      if (s === -1) {
        this.tableData.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row, true)
        })
      }
      if (s === 0) {
        this.tableData.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      }
      if (s === 1) {
        this.$refs.multipleTable.clearSelection()
      }
    },
    async handleRecover(index, row) {
      this.$confirm('确认要从回收站恢复吗', { type: 'warning' }).then(async() => {
        try {
          this.pload = false
          await api.commentRecover({ id: row.id })
          this.$notify({ title: '恢复成功', type: 'success', duration: 5000 })
          this.getData()
        } catch (e) {
          this.pload = true
        }
      })
    },
    async handleDelete(index, row) {
      this.$confirm('确认要从回收站删除吗', { type: 'warning' }).then(async() => {
        try {
          this.pload = false
          await api.commentRemove({ id: row.id })
          this.$notify({ title: '删除成功', type: 'success', duration: 5000 })
          this.getData()
        } catch (e) {
          this.pload = true
        }
      })
    },
    handleEdit(index, row) {
      this.selectTable = JSON.parse(JSON.stringify(row))
      this.selectTable.method = 'edit'
      this.edit = true
    },
    async getData() {
      const data = { pageNum: this.currentPage, pageSize: this.limit, fromUserId: this.userId, ...this.search }
      this.pload = false
      const Datas = await api.getCommentList(data)
      this.tableData = Datas.list
      this.count = Datas.total
      this.pload = true
    },
    handleFilter() {
      this.currentPage = 0
      this.getData()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.limit = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getData()
    }
  }
}
</script>
